package org.example.service.impl;

import org.example.dao.DepartmentDao;
import org.example.dao.TeacherDao;
import org.example.dto.MajorDto;
import org.example.dto.TeacherDto;
import org.example.pojo.Department;
import org.example.pojo.Major;
import org.example.pojo.Teacher;
import org.example.service.TeacherService;
import org.example.utils.Tool;
import org.example.vo.MajorVo;
import org.example.vo.TeacherVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Resource
    private TeacherDao dao;
    private DepartmentDao departmentDao;
    private Tool tool=new Tool();

    private MajorVo PoTransToVo(Major major){
        MajorVo majorVo=new MajorVo();
        majorVo.setMajorId(major.getMajorno());
        majorVo.setMajorName(major.getMajorname());
        majorVo.setDepartment(major.getDepartname());
        return majorVo;
    }

    private TeacherDto PoTransToDto(Teacher teacher){
        System.out.println(teacher);
        TeacherDto teacherDto=new TeacherDto();
        teacherDto.setTeacherName(teacher.getName());
        teacherDto.setTeacherId(teacher.getAccount());

//        int deparno=Integer.valueOf(String.valueOf(teacher.getAccount()).substring(2,4));
//        Department department=new Department();
//        department.setDepartno(deparno);
//        String deparname=departmentDao.queryByCondition(department).get(0).getDepartname();
        teacherDto.setDepartment("软件工程");
        teacherDto.setEmail(teacher.getEmail());
        teacherDto.setBirthday(teacher.getJoindate());
        teacherDto.setPhone(teacher.getTelenumber());
        teacherDto.setPosition(tool.checkTeacherString(teacher.getProf()));
        teacherDto.setAddress("0");
        teacherDto.setUnionAddress("0");
        teacherDto.setCity("厦门");
        teacherDto.setDistrict("集美");
        teacherDto.setProvince("福建");
        teacherDto.setTeacherId(teacher.getAccount());
        return teacherDto;
    }
    private Major DtoTransToPo(MajorDto majorDto){
        Major major=new Major();
        major.setMajorno(majorDto.getMajorId());
        major.setMajorname(majorDto.getMajorName());
        major.setDepartname(majorDto.getDepartment());
        return major;
    }
    private Teacher VoTransToPo(TeacherVo teacherVo){
        Teacher teacher=new Teacher();
        teacher.setAccount(teacherVo.getTeacherId());
        teacher.setEmail(teacherVo.getEmail());
        teacher.setName(teacherVo.getTeacherName());
        teacher.setTelenumber(teacherVo.getPhone());
        teacher.setProf(tool.checkTeacherint(teacherVo.getPosition()));
        return teacher;
    }

    @Override
    public Integer addTeacher(TeacherVo teacher) {
        if(dao.insert(VoTransToPo(teacher)))
            return 1;
        else
            return 0;
    }

    @Override
    public Integer updateTeacher(TeacherVo teacher) {
        if(dao.update(VoTransToPo(teacher)))
            return 1;
        else
            return 0;
    }

    @Override
    public Integer deleteTeacher(Integer teacherId) {
        if(dao.delete(teacherId))
            return 1;
        else
            return 0;
    }

    @Override
    public List<TeacherDto> queryAllTeacher() {
        List<Teacher> teacherList=dao.queryAll();
        List<TeacherDto> teacherDtoList=new ArrayList<>();
        for(int i=0;i<teacherList.size();i++)
            teacherDtoList.add(i,PoTransToDto(teacherList.get(i)));
        return teacherDtoList;
    }

    @Override
    public Integer countTeacher() {
        return dao.queryAll().size();
    }

    @Override
    public TeacherDto queryTeacherByName(String teacherName) {
        Teacher teacher=new Teacher();
        teacher.setName(teacherName);
        return PoTransToDto(dao.queryByCondition(teacher).get(0));
    }

    @Override
    public TeacherDto queryTeacherById(Integer teacherId) {
        Teacher teacher=new Teacher();
        teacher.setAccount(teacherId);
        return PoTransToDto(dao.queryByCondition(teacher).get(0));
    }
}
